package servlet;

import com.alibaba.fastjson.JSON;
import pojo.Tclassify;
import service.Impl.TclassifyserviceImpl;
import service.Tclassifyservice;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/tclassify/*")
public class TclassifyServlet extends HttpServlet {
    Tclassifyservice t1 = new TclassifyserviceImpl();


    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        //获取路径
        String rURI = request.getRequestURI();
        //拆分字符串截取路径
        String[] split = rURI.split("/");
        String ss = split[split.length - 1];
        //细分
        String[] split1 = ss.split("\\?");
        String s = split1[0];
        if ("selall".equals(s)) {
            this.doselall(request, response);
        }else if("sel".equals(s)){
            this.dosel(request,response);
        }
    }

    private void dosel(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        List<Tclassify> sel = t1.sel(Integer.parseInt(id));
        String s = JSON.toJSONString(sel);
        response.getWriter().print(s);
    }

    //获取三级菜单
    private void doselall(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Tclassify selall = t1.selall();
        request.setAttribute("sel", selall);
        request.getRequestDispatcher("/index.jsp").forward(request, response);
    }
}
